package com.example.sulingorder.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sulingorder.entity.OrderEntity;
import com.example.sulingorder.service.OrderService;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;


/**
 * 订单表的Controller
 *
 * @author wen
 * @email 1322127758@qq.com
 * @date 2022-03-11 14:25:53
 */
@RestController
@RequestMapping("sulingoxqg/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sulingoxqg:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取用户所有订单 对应的订单详细信息
     */
    @RequestMapping("/list/orders")
    public R getOrderList(@RequestParam("username")String username) throws ExecutionException, InterruptedException {
        if(!StringUtils.isEmpty(username)){
            return orderService.getOrderList(username);
        }
        return R.error("要查询的用户号为空");
    }

    /**
     * 获取订单数据,每次获取10个数据, 对应的订单详细信息
     * @param username :获取用户发布的订单数据
     * @param state :获取的订单类型
     * @param size  :当前数据起点
     * @return :订单数据
     */
    @RequestMapping("/list/orders/pagi")
    public R getOrderListPagi(@RequestParam("username")String username,@RequestParam(value = "state", required = false)Integer state,@RequestParam("size")Integer size) throws ExecutionException, InterruptedException {
        if(!StringUtils.isEmpty(username)){
            return orderService.getOrderListPagi(username,state,size);
        }
        return R.error("要查询的用户号为空");
    }

    /**
     * TODO 将用户联系方式截取替换成 *********
     * 获取用户领取的订单信息, 获取用户领取的全部订单数据, 可查全部,可查指定订单
     * @param orderSn:  要获取的订单号数据
     * @param username: 要查询哪个用户的订单
     * @param sendUsername: 发布订单的用户号 不一定存在, 存在就是查询指定订单
     * @param orOrderState: 要查询的订单状态, 不一定存在, 存在就是查询指定状态
     */
    @RequestMapping("/receive/orderdataall")
    public R getReceiveOrderDataEncryption(@RequestParam(value = "orderSn", required = false)String orderSn,@RequestParam("username")String username,@RequestParam(value = "sendUsername", required = false)String sendUsername,@RequestParam(value = "orOrderState",required = false)Integer orOrderState) throws ExecutionException, InterruptedException {
        if(!StringUtils.isEmpty(orderSn)){
            orderSn = orderSn.replace("\"","");
        }
        return orderService.getReceiveOrderData(orderSn,username, sendUsername,orOrderState);
    }

    /**
     * hint 用户点击查看完整信息, 不截取联系方式
     * 获取用户领取的订单信息,   获取用户领取的全部订单数据, 可查全部,可查指定订单
     * @param orderSn:      要获取的订单号数据
     * @param username:     要查询哪个用户的订单
     * @param sendUsername: 发布订单的用户号
     * @param orOrderState: 要查询的订单状态, 不一定存在, 存在就是查询指定状态
     */
    @RequestMapping("/receive/noencryption/orderdataall")
    public R getReceiveOrderDataNoEncryption(@RequestParam(value = "orderSn")String orderSn,@RequestParam("username")String username,@RequestParam(value = "sendUsername")String sendUsername,@RequestParam(value = "orOrderState",required = false)Integer orOrderState) throws ExecutionException, InterruptedException {
        if(!StringUtils.isEmpty(orderSn)){
            orderSn = orderSn.replace("\"","");
        }

        return orderService.getReceiveOrderData(orderSn,username, sendUsername,orOrderState);
    }

    /**
     * 作用: 用户领取订单
     * @param orderSn:   要领取的订单号
     * @param orMemberId:发布订单的用户号
     * @param username:  领取人的用户号
     * @param formCode:  防刷code
     */
    @PostMapping("/receive/order")
    public R upDataReceiveOrder(String orderSn,String orMemberId,String username,String formCode) throws ExecutionException, InterruptedException {

        return orderService.upDataReceiveOrder(orderSn, orMemberId, username, formCode);
    }


    /**
     * 发布者 确认收货, 扫码后发送数据过来
     * @param memberId :发布者号
     * @param userName :领取者号
     * @param orderSn  :要收货的订单号
     * @param code     :生成二维码时的code
     * @return :返回状态
     */
    @PostMapping("/confirm/receipt")
    public R haveReceipt(String memberId, String userName, String orderSn, String code) throws Exception {

        return orderService.haveReceipt(memberId, userName, orderSn, code);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orId}")
    //@RequiresPermissions("sulingoxqg:order:info")
    public R info(@PathVariable("orId") Long orId){
		OrderEntity order = orderService.getById(orId);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sulingoxqg:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sulingoxqg:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 按订单号修改支付状态, 支付状态 null/0没支付 1支付 2过期 3退款
     */
    @RequestMapping("/update/parstate")
    //@RequiresPermissions("sulingoxqg:order:update")
    public R updatePayState(@RequestParam("orOrderSn")String orOrderSn,@RequestParam("parSuccess")String parSuccess){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setParSuccess(Integer.valueOf(parSuccess));

        orderService.update(orderEntity, new QueryWrapper<OrderEntity>().eq("or_order_sn",orOrderSn));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sulingoxqg:order:delete")
    public R delete(@RequestBody Long[] orIds){
		orderService.removeByIds(Arrays.asList(orIds));

        return R.ok();
    }

}
